using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using System.Diagnostics;


namespace vXCopy
{
	class UtilLibrary
	{
		static public string GetFileNameOnly(string sFileName)
        {
            string[] sFile;
            string sFileNameOnly;
            char s = char.Parse("\\");
            try
            {
                sFile = sFileName.Split(s);
                sFileNameOnly = sFile[sFile.Length - 1];

                return sFileNameOnly;
            }
            catch (Exception ex)
            {
                /**/
                throw ex;
            }

        }
        static public bool CheckFolder(string Folder)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(Folder);

            //determine if the directory exists
            return Directory.Exists(dirInfo.FullName);
        }

        static public bool CheckFolder(DirectoryInfo dirInfo)
        {
            //determine if the directory exists
            return Directory.Exists(dirInfo.FullName);
        }

        static public bool CheckCreateFolder(string Folder, bool Prompt)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(Folder);
            return CheckCreateFolder(dirInfo, Prompt);
        }

        static public bool CheckCreateFolder(DirectoryInfo dirInfo, bool Prompt)
        {
            //determine if the destination directory exists, if not create it
            if (Directory.Exists(dirInfo.FullName))
            { return true; }
            else
            {
                // Directory doesn't exist
                // Check Prompt
                if (!Prompt)
                {   // Create folder with no prompt
                    Directory.CreateDirectory(dirInfo.FullName);
                    return true;
                }
                else
                {
                    // Prompt Message
                    Debug.WriteLine("[{0}] doesn't exist\nDo want to create it ? (Y/N)", dirInfo.FullName);
                    if (Console.ReadLine().ToUpper().IndexOf("Y") != -1)
                    {
                        Directory.CreateDirectory(dirInfo.FullName);
                        return true;
                    }
                    else
                    { return false; }
                }
            }
        }

		static public string CreateRegExFromString(string Value)
		{
			//*.doc;*.pdf;*.txt -> .(?:jpg|bmp|gif|doc|xls)
			//*.* -> .(?:.*)
			//*Test* -> (?:^Test1|^Test2|^Test.)
			
			bool bExt = false;
			StringBuilder sb = null;

			//[blank] 
			if (Value.Length == 0)
			{
				return string.Empty;
			}

			//*.* -> .(?:.*)
			if (Value.IndexOf("*.*") != -1)
			{
				sb = new StringBuilder(".(?:");
				sb.Append(".*");

				if (Value.IndexOf(";") == -1)
				{
					sb.Append(")");
					return sb.ToString();
				}
				else
				{
					sb.Append("|");
				}
			}

			//Test;Test1*;*Test2*
			if (Value.IndexOf(".") == -1)
			{
				sb = new StringBuilder("(?:");
				bExt = false;
			}
			else
			{
				sb = new StringBuilder(".(?:");
				bExt = true;
			}

			String[] exList = Value.Split(new Char[] { ';' });

			foreach (string ex in exList)
			{
				if (bExt)
				{
					sb.Append(ex.Replace("*.", ""));
				}
				else
				{
					sb.Append("^");
					sb.Append(ex.Replace("*.", ""));
				}
				
				sb.Append("|");
			}

			int nOffset = sb.ToString().LastIndexOf("|");
			sb.Remove(nOffset, 1);
			sb.Append(")");

			return sb.ToString();
		}
    }
}


